//
//  smPaymentController.m
//  shimaoHouse
//
//  Created by yst yst on 12-7-3.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smPaymentController.h"
#import "smContractSearchController.h"
#import "smMboSmTbPayment.h"

@interface smPaymentController ()<UITableViewDelegate,UITextFieldDelegate>


@property (nonatomic, strong) UILabel* palyLabel;
@property (nonatomic, strong) UISwitch* leftSwitch;
@property (nonatomic, strong) UISwitch* middleSwitch;
@property (nonatomic, strong) UISwitch* rightSwitch;
@property (nonatomic, strong) UITextField* totalMomey;



@end

@implementation smPaymentController
@synthesize palyLabel = _palyLabel;
@synthesize leftSwitch = _leftSwitch;
@synthesize rightSwitch = _rightSwitch;
@synthesize middleSwitch = _middleSwitch;
@synthesize totalMomey = _totalMomey;







- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
//    
//    UILabel* programnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
//    [programnameLabel setBackgroundColor:[UIColor colorWithRed:41/255.0 green:171/255.0 blue:226/255.0 alpha:1.0]];
//    [programnameLabel setText:@"合同编号：A100-1-30"];
//    [self.view addSubview:programnameLabel];
//    
 
//    
//    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(280, 10, 200, 30)];
//    [tempLabel setBackgroundColor:[UIColor clearColor]];
//    [tempLabel setText:@"世贸房产锦绣皇城"];
//    [self.view addSubview:tempLabel];
//    
//    
//    UIImage* tempImg = [UIImage imageNamed:@"hourse.jpg"];
//    UIImageView* imgView = [[UIImageView alloc] initWithImage:tempImg];
//    [imgView setFrame:CGRectMake(120, 210, 200, 180)];
//    [self.view addSubview:imgView];
//    
    
    
    
    //点击其他地方键盘消失
    UIButton* backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setFrame: self.view.bounds];
    [backBt setBackgroundColor:[UIColor clearColor]];
    //定义点击时的响应函数
	[backBt addTarget:self action:@selector(missKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBt];
    
    
    //后退
    UIButton* backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(5, 5, 118, 52);
    [backButton setImage:[UIImage imageNamed:@"back_button_up.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_button_down.png"] forState:UIControlStateHighlighted];
    backButton.backgroundColor=[UIColor clearColor];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: backButton];
    
    
    
//    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(310, 110, 400, 30)];
//    [tempLabel setBackgroundColor:[UIColor clearColor]];
//    [tempLabel setText:@"世贸房产锦绣皇城"];
//    tempLabel.font = [UIFont systemFontOfSize:28];
//    tempLabel.textColor=[UIColor colorWithRed:78/255.0 green:103/255.0 blue:156/255.0 alpha:1.0];
//    
//    [self.view addSubview:tempLabel];
    
    
    
    UILabel* tempLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(365, 250, 400, 40)];
    [tempLabel1 setBackgroundColor:[UIColor clearColor]];
    [tempLabel1 setText:@"合同编号：A100-1-30"];
    tempLabel1.font = [UIFont systemFontOfSize:22];
    tempLabel1.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel1];
    
    UILabel* tempLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(365, 290, 300, 40)];
    [tempLabel2 setBackgroundColor:[UIColor clearColor]];
    [tempLabel2 setText:@"创建时间：2012年6月30日"];
    tempLabel2.font = [UIFont systemFontOfSize:22];
    tempLabel2.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel2];
    
    UILabel* tempLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(365, 330, 300, 40)];
    [tempLabel3 setBackgroundColor:[UIColor clearColor]];
    [tempLabel3 setText:@"修改时间：2012年6月30日"];
    tempLabel3.font = [UIFont systemFontOfSize:22];
    tempLabel3.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel3];
    
    UILabel* tempLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(365, 370, 200, 40)];
    [tempLabel4 setBackgroundColor:[UIColor clearColor]];
    [tempLabel4 setText:@"服务人：王小子"];
    tempLabel4.font = [UIFont systemFontOfSize:22];
    tempLabel4.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel4];
    
    
    UILabel* tempLabel6 = [[UILabel alloc] initWithFrame:CGRectMake(365, 410, 200, 40)];
    [tempLabel6 setBackgroundColor:[UIColor clearColor]];
    [tempLabel6 setText:@"收款金额："];
    tempLabel6.font = [UIFont systemFontOfSize:22];
    tempLabel6.textColor=[UIColor whiteColor];
    [self.view addSubview:tempLabel6 ];
    
    self.totalMomey = [[UITextField alloc] initWithFrame:CGRectMake(470, 410, 200, 35)];
    [self.totalMomey setPlaceholder:@"请输入总金额"];
    [self.totalMomey setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.totalMomey setDelegate:self];
    [self.view addSubview: self.totalMomey];
    
    
    
    
    UILabel* contratPayLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 470, 100, 50)];
    [contratPayLabel setText:@"付款方式:"];    
    [contratPayLabel setBackgroundColor:[UIColor clearColor]];
    contratPayLabel.font = [UIFont systemFontOfSize:22];
    contratPayLabel.textColor=[UIColor whiteColor];
    //加载lable
    [self.view addSubview: contratPayLabel];
    
    
    UILabel* leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(215, 470, 80, 50)];
    [leftLabel setText:@"未收款"];    
    [leftLabel setBackgroundColor:[UIColor clearColor]];
    leftLabel.font = [UIFont systemFontOfSize:22];
    leftLabel.textColor=[UIColor whiteColor];
    //加载lable
    [self.view addSubview:leftLabel];
    
    
    self.leftSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(290, 480, 50, 50)];
    
    [self.leftSwitch addTarget:self action:@selector(switchLeftAction:) forControlEvents:UIControlEventValueChanged];
    self.leftSwitch.on = YES;
    // in case the parent view draws with a custom color or gradient, use a transparent color
    self.leftSwitch.backgroundColor = [UIColor clearColor];
    [self.leftSwitch setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
    [self.view addSubview:self.leftSwitch];
    
    UILabel* middleLabel = [[UILabel alloc] initWithFrame:CGRectMake(395, 470, 100, 50)];
    [middleLabel setText:@"部分收款"];    
    [middleLabel setBackgroundColor:[UIColor clearColor]];
    middleLabel.font = [UIFont systemFontOfSize:22];
    middleLabel.textColor=[UIColor whiteColor];    //加载lable
    [self.view addSubview:middleLabel];
    
    self.middleSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(485, 480, 50, 50)];
    
    [self.middleSwitch addTarget:self action:@selector(switchMiddleAction:) forControlEvents:UIControlEventValueChanged];
    
    // in case the parent view draws with a custom color or gradient, use a transparent color
    self.middleSwitch.backgroundColor = [UIColor clearColor];
    
    [self.middleSwitch setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
    
    [self.view addSubview:self.middleSwitch];
    
    
    UILabel* rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(590, 470, 100, 50)];
    [rightLabel setText:@"全额收款"];    
    [rightLabel setBackgroundColor:[UIColor clearColor]];
    rightLabel.font = [UIFont systemFontOfSize:22];
    rightLabel.textColor=[UIColor whiteColor];    //加载lable    //加载lable
    [self.view addSubview:rightLabel];
    
    self.rightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(680, 480, 50, 50)];
    
    [self.rightSwitch addTarget:self action:@selector(switchRightAction:) forControlEvents:UIControlEventValueChanged];
    
    // in case the parent view draws with a custom color or gradient, use a transparent color
    self.rightSwitch.backgroundColor = [UIColor clearColor];
    
    [self.rightSwitch setAccessibilityLabel:NSLocalizedString(@"StandardSwitch", @"")];
    
    [self.view addSubview:self.rightSwitch];
    
    
    
    
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setFrame:CGRectMake(26, 760,282, 73)];
    [cancelButton setImage:[UIImage imageNamed:@"cancel_button_up.png"] forState:UIControlStateNormal];
    [cancelButton setImage:[UIImage imageNamed:@"cancel_button_down.png"] forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(cancleClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
    
    
    UIButton* tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setFrame:CGRectMake(462, 760, 282, 73)];
    [tempButton setImage:[UIImage imageNamed:@"comfirm_button_up.png"] forState:UIControlStateNormal];
    [tempButton setImage:[UIImage imageNamed:@"comfirm_button_down.png"] forState:UIControlStateHighlighted];
    [tempButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tempButton];
    

    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"payment.png"]]];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}




//确认
-(void) buttonClick:(UIButton*) bt {
    
    
    @try { 
        NSString * mode=@"";
        //未收款
        if(self.leftSwitch.isOn){
            mode = @"0";
        //部分收款
        }else if(self.middleSwitch.isOn){
            mode = @"1";
        }
        //全部收款
        else {
            mode = @"2";
        }
        
        smMboSmTbPayment * payment = [smMboSmTbPayment getInstance];
        [payment setReceivables_amount:self.totalMomey.text];
        [payment setMode_payment:mode];
        [payment create:([NSNumber numberWithInt:[@"500" intValue]])];    
        [payment submitPending];
        [smMboSmMboDB synchronize];
        
    }
    @catch (NSException *exception) {
        NSLog(@"%@: %@", [exception name],[exception reason]);
        return;
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"付款：确认信息" message:@"付款成功"
                                                delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];	
    
//    smContractSearchController* contractSearchController = [[smContractSearchController alloc] init];    
//    [self.navigationController pushViewController:contractSearchController animated:YES ]; 

}

//取消
-(void) cancleClick:(UIButton*) bt {     
    [self.navigationController popViewControllerAnimated:YES];
}


//返回
-(void) backClick:(UIButton*) bt {     
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)switchLeftAction:(id)sender{
    UISwitch *mySwitch = (UISwitch *)sender;
    BOOL setting = mySwitch.isOn;	//获得开关状态
    if(setting){
        [self.rightSwitch setOn:FALSE];//设置开关状态
        [self.middleSwitch setOn:FALSE];
    }
}
- (void)switchMiddleAction:(id)sender{
    UISwitch *mySwitch = (UISwitch *)sender;
    BOOL setting = mySwitch.isOn;	//获得开关状态
    if(setting){
        [self.rightSwitch setOn:FALSE];//设置开关状态
        [self.leftSwitch setOn:FALSE];
    }
}
- (void)switchRightAction:(id)sender{
    UISwitch *mySwitch = (UISwitch *)sender;
    BOOL setting = mySwitch.isOn;	//获得开关状态
    if(setting){
        [self.leftSwitch setOn:FALSE];
        [self.middleSwitch setOn:FALSE];
    }
}


-(void) missKeyBoard {
    [self.totalMomey resignFirstResponder]; 
}





@end
